package action.chap3;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class ExecuteAround {
    public static void main(String[] args) throws IOException {
        // method we want to refactor to make more flexible
        String result = processFileLimited();
        System.out.println(result);

        System.out.println("---");
        //这里还可以优化 后期学习 BufferedReader执行一个行为
        String oneLine = processFile((BufferedReader b) -> b.readLine());
        System.out.println(oneLine);
        //BufferedReader执行两个行为
        String twoLines = processFile((BufferedReader b) -> b.readLine() + b.readLine());
        System.out.println(twoLines);
    }

    public static String processFileLimited() throws IOException {
        try (BufferedReader br =
                     new BufferedReader(new FileReader("F:\\study\\study-java8\\src\\main\\resources\\study-java8\\data.txt"))) {
            return br.readLine();
        }
    }


    public static String processFile(BufferedReaderProcessor p) throws IOException {
        try(BufferedReader br = new BufferedReader(new FileReader("F:\\study\\study-java8\\src\\main\\resources\\study-java8\\data.txt"))){
            return p.process(br);
        }

    }
    @FunctionalInterface
    public interface BufferedReaderProcessor{
         String process(BufferedReader b) throws IOException;

    }
}
